#include <iostream>
#include <string>
#include <limits>
#include <cstdlib>
#include <windows.h>
#include <algorithm>
#include <iomanip>

#include "../include/main_core.hh"
#include "../include/main_customer.hh"
#include "../include/main_employee.hh"
#include "../include/main_admin.hh"
#include "../include/main_auditor.hh"
#include "../include/main_utils.hh"

// 添加全局AuthManager变量的定义
AuthManager authManager;

// 主函数核心实现
int main_core() {
    // 设置控制台代码页为UTF-8
    SetConsoleOutputCP(65001);  // 65001是UTF-8的代码页
    SetConsoleCP(65001);        // 同时设置输入代码页
    
    // 创建系统核心组件
    BankSystem bankSystem(authManager);
    UIManager uiManager(authManager);
    
    // 欢迎界面
    clearScreen();
    std::cout << "===========================" << std::endl;
    std::cout << "   " << uiManager.getLocalizedText("WELCOME") << "   " << std::endl;
    std::cout << "===========================" << std::endl << std::endl;
    
    uiManager.getInput(uiManager.getLocalizedText("PRESS_ENTER"));
    
    while (true) {
        if (!authManager.isAuthenticated()) {
            // 显示登录界面
            clearScreen();
            std::cout << "===== " << uiManager.getLocalizedText("LOGIN_TITLE") << " =====" << std::endl << std::endl;
            
            std::cout << "1. " << uiManager.getLocalizedText("REGISTER_NEW") << std::endl;
            std::cout << "2. " << uiManager.getLocalizedText("LOGIN_EXISTING") << std::endl;
            std::cout << "3. " << uiManager.getLocalizedText("SETTINGS") << std::endl;
            
            int loginChoice = uiManager.getMenuChoice(1, 3);
            
            switch (loginChoice) {
                case 1: // 注册新客户
                    handleCustomerRegistration(bankSystem, uiManager);
                    break;
                    
                case 2: { // 登录现有账户
                    std::string username = uiManager.getInput(uiManager.getLocalizedText("ENTER_USERNAME"));
                    std::string password = uiManager.getPassword(uiManager.getLocalizedText("ENTER_PASSWORD"));
                    
                    // 尝试登录
                    auto customer = bankSystem.findCustomerByName(username);
                    if (customer && customer->verifyPassword(password)) {
                        authManager.authenticateAsCustomer(username, password);
                        std::cout << uiManager.getLocalizedText("LOGIN_SUCCESS") << std::endl;
                        uiManager.getInput(uiManager.getLocalizedText("PRESS_ENTER"));
                    } else {
                        std::cout << uiManager.getLocalizedText("LOGIN_FAILED") << std::endl;
                        uiManager.getInput(uiManager.getLocalizedText("PRESS_ENTER"));
                    }
                    break;
                }
                    
                case 3: // 设置
                    uiManager.showSettingsMenu();
                    break;
            }
        } else {
            // 根据角色处理相应的菜单
            clearScreen();
            
            // 直接进入相应的角色菜单，而不显示主菜单
            switch (authManager.getCurrentUserRole()) {
                case Role::CUSTOMER:
                    handleCustomerMenu(bankSystem, uiManager);
                    break;
                case Role::EMPLOYEE:
                    handleEmployeeMenu(bankSystem, uiManager);
                    break;
                case Role::ADMIN:
                    handleAdminMenu(bankSystem, uiManager);
                    break;
                case Role::AUDITOR:
                    handleAuditorMenu(bankSystem, uiManager);
                    break;
            }
            
            // 询问是否退出
            clearScreen();
            if (uiManager.getCurrentLanguage() == Language::ENGLISH) {
                std::cout << "Do you want to exit the system?" << std::endl;
                std::cout << "A. Exit" << std::endl;
                std::cout << "B. Return to Settings" << std::endl;
                std::cout << "C. Logout" << std::endl;
                std::cout << "D. Continue" << std::endl;
            } else if (uiManager.getCurrentLanguage() == Language::FRENCH) {
                std::cout << "Voulez-vous quitter le système?" << std::endl;
                std::cout << "A. Quitter" << std::endl;
                std::cout << "B. Retourner aux paramètres" << std::endl;
                std::cout << "C. Déconnexion" << std::endl;
                std::cout << "D. Continuer" << std::endl;
            } else {
                std::cout << "是否退出系统？" << std::endl;
                std::cout << "A. 退出" << std::endl;
                std::cout << "B. 返回设置菜单" << std::endl;
                std::cout << "C. 注销登录" << std::endl;
                std::cout << "D. 继续使用" << std::endl;
            }
            
            char choice = uiManager.getKeyChoice("ABCD");
            
            if (choice == 'A') {
                break; // 退出系统
            } else if (choice == 'B') {
                uiManager.showSettingsMenu(); // 返回设置菜单
            } else if (choice == 'C') {
                authManager.logout(); // 注销登录
            }
            // 如果选择D，继续使用，回到主循环
        }
    }
    
    // 退出信息
    clearScreen();
    
    // 保存所有数据到文件
    bankSystem.saveData();
    
    if (uiManager.getCurrentLanguage() == Language::ENGLISH) {
        std::cout << "Thank you for using our system. Goodbye!" << std::endl;
    } else if (uiManager.getCurrentLanguage() == Language::FRENCH) {
        std::cout << "Merci d'avoir utilisé notre système. Au revoir!" << std::endl;
    } else {
        std::cout << "谢谢使用，再见！" << std::endl;
    }
    
    return 0;
} 